﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WEALTH.Manager.BaseSystem
{
    public partial class GroupMenus : BasePage
    {
        ExcuteDao groupment = new ExcuteDao();
        ExcuteDao menus = new ExcuteDao();
        public string GroupMenu = "", MenuCode = "";
        public string strSessCode = "", strSql = "", strLink = "", strMsg = "";
        public int n = 0;

        protected void Page_Load(object sender, EventArgs e)
        {

            strSessCode = Request.QueryString["code"].ToString().Trim();
            if (!IsPostBack)
            {
                TreeView_Menu.Attributes.Add("onclick", "postBackByObject()");
                GroupMenu = groupment.GetRGroupNameReader(strSessCode);

                ShowTree();
            }
        }

        public void ShowTree()
        {
            TreeView_Menu.Nodes.Clear();

            TreeNode node_0 = new TreeNode();
            node_0.ImageUrl = "../Images/Small/calendar.gif";
            node_0.Text = groupment.GetGroupNameReader(strSessCode).ToString().Trim();
            node_0.Value = "1";

            node_0.SelectAction = TreeNodeSelectAction.Expand;
            node_0.Expanded = true;
            this.TreeView_Menu.Nodes.Add(node_0);

            this.ViewState["ds"] = menus.GetQueryMenu();

            AddTree(node_0.Value, node_0);

        }

        public void AddTree(String ParentID, TreeNode pNode)
        {
            DataSet ds = (DataSet)this.ViewState["ds"];
            DataView dvTree = new DataView(ds.Tables[0]);
            dvTree.RowFilter = " Menu_Pcode = '" + ParentID + "'";

            foreach (DataRowView Row in dvTree)
            {
                TreeNode Node = new TreeNode();
                //添加当前节点的子节点
                Node.Value = Row["Menu_Code"].ToString().Trim();
                Node.Text = Row["Menu_Name"].ToString().Trim() + "<a herf='#'></a>";
                Node.Text += " <font style='font-size: 11px' color='#808080'>[" + Node.Value.ToString().Trim() + "]</font> ";


                if (Row["Menu_FileName"].ToString().Trim().Length != 0)
                {
                    Node.Text += "<font style='font-size: 11px' color='#0000ff'>" + Row["Menu_FileName"].ToString().Trim() + "</font> ";
                }

                //if (Row["IsVisible"].ToString().Trim() != "1")
                //{
                //    Node.Text += " <font color='#ff0000'>[隐藏]</font> ";
                //}

                //if (Row["Menu_IsUse"].ToString().Trim() != "1")
                //{
                //    Node.Text += " <font color='#ff0000'>[停用]</font> ";
                //}


                pNode.ChildNodes.Add(Node);
                Node.Expanded = false;
                if (Row["Menu_Ico"].ToString().Trim() == "")
                {
                    Node.ImageUrl = "../images/Small/notes.gif";
                }
                else
                {
                    Node.ImageUrl = "../images/Small/" + Row["Ico"].ToString().Trim();
                }


                MenuCode = "'" + Row["Menu_Code"].ToString().Trim() + "'";
                if (GroupMenu.IndexOf(MenuCode) > -1)
                {
                    Node.Checked = true;
                    pNode.Checked = true;
                }

                if (pNode != null)
                {
                    MenuCode = "'" + Row["Menu_Code"].ToString().Trim() + "'";

                }

                AddTree(Row["Menu_Code"].ToString(), Node);    //再次递归

            }


        }

        string FunctionString = "";
        public string GetTreeCheck(TreeNode tn)
        {
            foreach (TreeNode treenode in tn.ChildNodes)
            {
                if (treenode.Checked)
                {
                    string va = treenode.Value;
                    string vb = treenode.Text;

                    FunctionString += "," + va + "";
                }
                else
                {
                }
                GetTreeCheck(treenode);//调用自身函数循环遍历TreeView

            }

            return FunctionString;//返回FunctionString字符串

        }

        protected void btnUpdate_Menu_Click(object sender, EventArgs e)
        {
            string tmpGroupCode = strSessCode;
            string tmpGroupMenu = GetTreeCheck(TreeView_Menu.Nodes[0]);

            //删除关系表中的该角色记录
            n = groupment.DelRGroupMenu(strSessCode);


            string[] paraGroupMenu = tmpGroupMenu.Split(',');
            if (paraGroupMenu.Length <= 1)
            {
                Session["msg"] = "执行成功！";
                Response.Write("<script>top.window.location='../Welcome.aspx?local=BaseSystem/RoleList.aspx';</script>");
            }
            else
            {
                for (int i = 0; i < paraGroupMenu.Length; i++)
                {
                    if (paraGroupMenu[i].ToString().Trim() != "")
                    {
                        n = groupment.InsGroupMenu(tmpGroupCode, paraGroupMenu[i].ToString().Trim());
                    }
                }
            }

            if (n > 0)
            {
                Session["msg"] = "执行成功！";
                Response.Write("<script>top.window.location='../Welcome.aspx?local=BaseSystem/RoleList.aspx';</script>");
            }
            else
            {
                ShowMsg("设置失败！", 1);
            }




            //return;
        }

        //当点击选择框时执行的事件
        protected void TreeView_Menu_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        {
            SetChildChecked(e.Node);
            SetParentChecked(e.Node);

        }
        //选择所有的父目录
        private void SetChildChecked(TreeNode parentNode)
        {
            foreach (TreeNode node in parentNode.ChildNodes)
            {
                node.Checked = parentNode.Checked;

                if (node.ChildNodes.Count > 0)
                {
                    SetChildChecked(node);
                }

            }
            //if (parentNode.ChildNodes.Count <= 0)
            //{
            //    parentNode.Parent.Checked = false;
            //}

        }

        //选择所有的子目录
        private void SetParentChecked(TreeNode childNode)
        {
            if (childNode.Parent != null)
            {
                if (childNode.Checked)
                {
                    childNode.Parent.Checked = true;
                }
                //else
                //{
                //    childNode.Parent.Checked = false;
                //}
                SetParentChecked(childNode.Parent);
            }
        }

    }
}